﻿Public Class MessageForm
    Private _messageList As New List(Of SMSMessage)
    Private _phone As BaseMobile
    Private _currentIndex As Integer

    Public Sub New(ByRef mobile As BaseMobile)
        InitializeComponent()
        _phone = mobile
        _messageList = _phone.MessageList
        Init()
    End Sub

    'закрытие формы
    Private Sub bClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClose.Click
        Me.Close()
    End Sub

    'инициализация
    Private Sub Init()
        LoadMessage(_messageList.Count - 1)
        _currentIndex = _messageList.Count - 1
    End Sub

    'загрузка сообщения по номеру
    Private Sub LoadMessage(ByVal i As Integer)
        Dim msg As Object = _messageList(i)

        'если сообщение - ММС, то загружаем для него картинку
        If (TypeOf msg Is MMSmessage) Then
            pbMMSpicture.Image = Image.FromFile(CType(msg, MMSmessage).PicturePath)
        Else
            pbMMSpicture.Image = Nothing
        End If
        tbFrom.Text = CType(msg, SMSMessage).From
        tbDateRecieved.Text = CType(msg, SMSMessage).MessageDate
        tbMessageText.Text = CType(msg, SMSMessage).Text
        CType(msg, SMSMessage).IsUnread = False
        If (CType(msg, SMSMessage).IsUnread = True) Then
            lblMessageIsRead.Text = "Не прочитано"
        Else
            lblMessageIsRead.Text = "Прочитано"
        End If

        _messageList(i).IsUnread = False
    End Sub

    'редактировать текст сообщения - нельзя
    Private Sub tbMessageText_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbMessageText.KeyPress
        e.KeyChar = ""
    End Sub

    'удаление сообщения
    Private Sub bDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bDelete.Click
        _messageList.RemoveAt(_currentIndex)
        If (_messageList.Count > 0) Then
            _currentIndex = _messageList.Count - 1
            LoadMessage(_messageList.Count - 1)
        Else
            'если все сообщения удалены - закрываем форму
            Me.Close()
        End If
    End Sub

    'предыдыущее сообщение
    Private Sub bPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrev.Click
        If (_currentIndex > 0) Then
            _currentIndex -= 1
        Else
            _currentIndex = _messageList.Count - 1
        End If
        LoadMessage(_currentIndex)
    End Sub

    'следующее сообщение
    Private Sub bNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bNext.Click
        If (_currentIndex < _messageList.Count - 1) Then
            _currentIndex += 1
        Else
            _currentIndex = 0
        End If
        LoadMessage(_currentIndex)
    End Sub

    Private Sub bMarkAsUnread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bMarkAsUnread.Click
        _messageList(_currentIndex).IsUnread = True
        lblMessageIsRead.Text = "Не прочитано"
    End Sub
End Class